import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileOutputStream;


public class Main {

	public static void main(String[] args) {
		String configPath = "config.xml";
		if(args.length > 0)
			configPath = args[0];
		BotConfig cfg = null;
		
		System.out.print("Icm Bot ver. 0.4 \n");
		
		XMLDecoder decoder = null;
		try
		{
			FileInputStream is = new FileInputStream(configPath);
			decoder = new XMLDecoder(is);
			cfg = (BotConfig)decoder.readObject();
		}
		catch (Exception e) {
			System.err.print(e.getMessage());
			System.err.print("\n");
		}
		finally
		{			
			if(decoder != null)
				decoder.close();
		}
		if(cfg == null)
		{
			XMLEncoder encoder = null;
			try
			{
				cfg = new BotConfig();
				cfg.MakeDefaults(false);
				FileOutputStream os = new FileOutputStream(configPath);
				encoder = new XMLEncoder(os);
				encoder.writeObject(cfg);
			}
			catch (Exception e) {
			}
			finally
			{
				if(encoder != null)
					encoder.close();
			}
			System.out.print("Brak pliku konfiguracji...");
			return;
		}
		
		try
		{
			Engine engine = new Engine(cfg);
			new Thread(engine).start();
			System.in.read();
			engine.stop();
			while(engine.isEnd() == false)
				Thread.sleep(500);
		}
		catch (Exception e) {
			System.out.print(e.getMessage());
			System.err.print("\n");
		}
			
	}

}
